const cloud = require('wx-server-sdk')

cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV,
})

exports.main = async (event) => {
	try {
		const db = cloud.database()

		let param = {}

		Object.keys(event).forEach((key) => {
			if (!['userInfo'].includes(key)) {
				param[key] = db.RegExp({
					regexp: '.*' + event[key],
					options: 'i',
				})
			}
		})

		const { data } = await db.collection('gn_article').where(param).get()

		return {
			data,
			code: 0,
			msg: '查询成功',
		}
	} catch (err) {
		throw err
	}
}
